<template>
<div class="container">
  <van-nav-bar title="我的订单" />
  <van-tabs v-model:active="active" @click-tab="onClickTab">
    <van-tab title="全部" name=""></van-tab>
    <van-tab title="待支付" name="1"></van-tab>
    <van-tab title="待服务" name="2"></van-tab>
    <van-tab title="已完成" name="3"></van-tab>
    <van-tab title="已取消" name="4"></van-tab>
  </van-tabs>
  <van-row @click="goDetail(item)" v-for="item in order" :key="item.out_trade_no">
    <van-col span="5">
      <van-image style="width: 50px; height: 50px;" :src="item.serviceImg" radius="5"/>
    </van-col>
    <van-col span="14">
      <div class="text1">{{ item.service_name }}</div>
      <div class="text2">{{ item.hospital_name }}</div>
      <div class="text2">{{ '预约时间:' + item.starttime }}</div>
    </van-col>
    <van-col class="text2 text3" span="5" :style="{color: colorMap[item.trade_state]}">
      {{ item.trade_state }}
      <counter :second="item.timer" v-if="item.trade_state === '待支付'"/>
    </van-col>
  </van-row>
  <div class="bottom-text">没有更多了</div>
</div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { getOrderList } from '../../api';
import counter from '../../components/counter.vue';
import { useRouter } from 'vue-router';
const colorMap = {
  '待支付': '#ffa200',
  '待服务': '#1da6fd',
  '已完成': '#21c521'
}
const order = ref([])
//获取订单列表API
const getOrderListAPI = async (state) => {
  const { data } = await getOrderList({ state })
  data.data.forEach((item) => {
    item.timer = item.order_start_time + 7200000 - Date.now()
  })
  order.value = data.data
}
//点击navBar时触发回调
const onClickTab = (item) => {
  getOrderListAPI(item.name)
  
}
const active = ref('')
const router = useRouter()
//点击跳转订单详情页
const goDetail = (item) => {
  router.push(`/detail?oid=${item.out_trade_no}`)
}
onMounted(() => {
  getOrderListAPI()
})
</script>

<style lang="less" scoped>
.container {
  background-color: #f0f0f0;
  height: 100vh;
}
.van-row {
  background-color: #fff;
  padding: 10px;
  margin: 5px;
  border-radius: 5px;
  .text1 {
    font-size: 16px;
    line-height: 25px;
    font-weight: bold;
  }
  .text2 {
    font-size: 14px;
    line-height: 20px;
    color: #999999;
  }

  .text3 {
    align-content: center;
  }
}
.bottom-text {
  line-height: 50px;
  text-align: center;
  color: #999999;
}
</style>